﻿using System;
using System.Linq;
using System.Collections.ObjectModel;
using System.Device.Location;
using System.Windows;

namespace Transportr
{
    public class LocationModel : BaseItemViewModel
    {
        private GeoCoordinateWatcher watcher;
        private GeoPosition<GeoCoordinate> position;
        private string mStatus;

        public ObservableCollection<StationItemViewModel> Items { get; private set; }
        public static GeoCoordinate CurrentLocation;
        public string FilterText { get; set; }
        public GeoCoordinate FakeLocation { get; set; }
    
        public ObservableCollection<LineItemViewModel> FilteredItems
        {
            get
            {
                var filteredItems = from item in Items
                                    where item.LineNumber.Contains(FilterText)
                                    select item;
                return filteredItems as ObservableCollection<LineItemViewModel>;
            }
        }

        public bool IsDataLoaded { get; private set; }

        public string Status
        {
            get { return mStatus; }
            set
            {
                if (value != mStatus)
                {
                    mStatus = value;
                    NotifyPropertyChanged("Status");
                }
            }
        }

        public bool StatusVisible { get { return this.mStatus.Length > 0; } }

        public LocationModel()
        {
            this.FakeLocation = new GeoCoordinate();
            this.position = new GeoPosition<GeoCoordinate>(DateTime.Now, new GeoCoordinate(47.47, 19.05));
            this.Items = new ObservableCollection<StationItemViewModel>();
            watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);
            watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
            watcher.Start();
            if (watcher.Status == GeoPositionStatus.Disabled || watcher.Status == GeoPositionStatus.NoData)
            {
                Status = "No location available.";
            }
        }

        void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            position = e.Position;
            this.BeginUpdate();
        }

        public void BeginUpdate()
        {
            IsDataLoaded = false;
            Service.SendRequest(ServiceModel.Function.StationLines, new ServiceModel.Parameters()
                {
                    stLat = position.Location.Latitude,
                    stLng = position.Location.Longitude
                }, EndUpdate, false);
        }

        void EndUpdate(object sender, Service.RequestState state)
        {
            System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    this.Items.Clear();
                    foreach (ServiceModel.StationLine sl in state.Response.StationLines.StationLine)
                    {
                        this.Items.Add(new StationItemViewModel()
                        {
                            RawData = sl
                        });
                    }
                    this.IsDataLoaded = true;
                });
        }
    }
}
